<?php
require_once('version.inc');
require_once('dbaccess.inc');
require_once('language.inc');
require_once('header.inc');
require_once('helper.inc');
require_once('crypt.inc');
require_once('settings.inc');

$drm = array(true, false, false, false, false, true, false, true);

$knopf = 0;
$func = $_REQUEST['func'];

# Auswertungsmenue
if (isset($_REQUEST['pjplan'])) { $knopf = 21; $headline = 27; }

require_once('menu.inc');
require_once('knumber.inc');

# Folgende Funktion erlaubt die Auswahl eines Projekts dessen Plan
# erzeugt werden soll.
#
function Preselect() {
	global $periode;
	global $unum;
	global $menu;
	global $rstufe;
	global $pmlight;
	global $dtformatshort;

	$db = OpenDB();
?>
<form name="preselect" action="printplan.php" method="post">
<input type="hidden" name="menu" value="<? echo "$menu"; ?>">
<input type="hidden" name="headline" value="27">
<input type="hidden" name="func" value="printplan">

<table class="input">
<?php
	# Auswahl eines Projekts
	echo "<tr><td>Projektauswahl:</td>\n";
	echo "<td>";

	$query = "select pl_num, pl_prnum, pl_lfd, pr_name, pl_status from ";
	$query .= "plan, project where pr_num = pl_prnum and pr_status = 0 ";
	$query .= "order by pl_prnum, pl_lfd desc";

	$result = QueryDB($db, $query);

	if (!$result) {
	   closeDB($db);
	   return;
	}

	echo "<select name=\"pl_num\">\n";
	$numrows = numrowsDB($result);
	$rows = 0;

	while ($rows < $numrows) {
	   $data = fetchDB($result, $rows);
	   $pl_num = $data[0];
	   $pl_prnum = $data[1];
	   $pl_lfd = $data[2];
	   $pr_name = $data[3];
	   $pl_status = $data[4];

	   switch ($pl_status) {
	      case 1: $stat = "In Erstellung"; break;
	      case 2: $stat = "Freigegeben"; break;
	      case 3: $stat = ""; break;
	      case 4: $stat = "Aktiv"; break;
	      case 5: $stat = "Inaktiv"; break;
	      case 6: $stat = "Abgeschlossen"; break;
	   }

	   echo "<option value=\"$pl_num\">$pl_lfd ($stat): $pl_prnum $pr_name</option>\n";
	   $rows++;
	}

	echo "</select>";
	echo "</td></tr>\n";
	echo "<tr><td>IST-Werte ber&uuml;cksichtigen bis:</td>\n";
	$sd = GetVisualDate($dtformatshort, true);

	if (!$pmlight) {
	   $dat = time();
	   $day = gmdate("j", $dat);
	   $mon = gmdate("n", $dat);
	   $year = gmdate("Y", $dat);
	   $dm = daysinmonth($mon, $year);

	   if ($day < ($dm / 2)) {
	      $mon--;

	      if ($mon < 1) {
	         $year--;
	         $mon = 12;
	      }
	   }

	   $vd = GetShortDate(gmmktime(0, 0, 0, $mon, daysinmonth($mon, $year), $year));
	} else
	   $vd = "";

	echo "<td><input type=\"text\" name=\"datum\" value=\"$vd\" size=7 maxlength=7> $sd</td></tr>\n";
	echo "</table>\n";
	echo "<table class=\"indent\"><tr><td>";
	ButtonSubmit("Senden", "preselect");
	echo "</td></tr></table>\n";
	echo "</form>\n";
	closeDB($db);
}

function GetWD_HOURS($db, $dt, $ressource, $hash) {
	global $leneinheit;

	$mon = gmdate("n", $dt);
	$year = gmdate("Y", $dt);
	$dta = gmmktime(0, 0, 0, $mon, 1, $year);
	$dte = gmmktime(0, 0, 0, $mon, daysinmonth($mon, $year), $year);
	$query = "select sum(wd_hours)/$leneinheit from wdone where ";
	$query .= "wd_minum = $ressource and ";
	$query .= "wd_datum between $dta and $dte and ";
	$query .= "wd_task in (select ta_num from task, allocation where ";
	$query .= "al_task = ta_num and ta_hash = '$hash' and ";
	$query .= "al_ressource = $ressource)";

	if (!($reswd = QueryDB($db, $query)))
	   return 0;

	if (numrowsDB($reswd)> 0) {
	   $data = fetchDB($reswd, 0);
	   $wd_hours = $data[0];
	} else
	   $wd_hours = 0;

	return $wd_hours;
}

function PrintMa($db, $dstart, $dend, $al_task, $color, $pl_num, $ressource) {
	global $periode;
	global $einheit;
	global $leneinheit;
	global $menu;
	global $role;
	global $pmlight;

	$nav = "pl_num=$pl_num&al_task=$al_task&menu=$menu&headline=20";
	$dt = $_REQUEST['datum'];

	if (isset($dt) && strlen($dt) > 0) {
	   $darr = explode(".", $dt);
	   $datum = gmmktime(0, 0, 0, $darr[0], daysinmonth($darr[0], $darr[1]), $darr[1]);
	} else
	   $datum = 0;

        $query = "select am_num, am_task, am_ressource, am_hours, am_pstart,";
        $query .= "am_pduration, am_role, am_krnum, am_nname, am_vname, am_phase ";
        $query .= "from allocmi where am_task = $al_task order by ";
        $query .= "am_nname, am_vname, am_pstart";
        $erg = QueryDB($db, $query);

        if (!$erg)
           return 0;

        $erganz = numrowsDB($erg);
        $ergrow = 0;
	$oressource = 0;
	$dt = 0;
	$sum = 0;
	$sumi = 0;
	$sumip = 0;
	$total = 0;

	while ($ergrow < $erganz) {
           $data = fetchDB($erg, $ergrow);
           $am_num = $data[0];
	   $am_task = $data[1];
           $am_ressource = $data[2];
           $am_hours = $data[3];
           $am_pstart = $data[4];
           $am_pduration = $data[5];
           $am_role = $data[6];
           $am_krnum = $data[7];
           $am_nname = $data[8];
           $am_vname = $data[9];
	   $am_phase = $data[10];

	   if ($oressource != $am_ressource) {
	      if ($oressource != 0) {
	         $ed = GetEDate($einheit, $ta_start, $ta_duration);

		 if ($dt < $ed) {
		    while (PerDateComp($dt, $ed) >= 0) {
		       if ($datum > 0 && $dt <= $datum) {
			  $wd_hours = GetWD_HOURS($db, $dt, $oressource, $ta_hash);
			  $sumip += $wd_hours;

			  if ($wd_hours > 0)
			     printf("<td style=\"background-color: #$color; border: 1px solid black;\">I:&nbsp;%s</td>\n",
				FormatNum($wd_hours, 3));
			  else
			     echo "<td style=\"background-color: #$color; border: 1px solid black;\">&nbsp;</td>\n";
		       } else
			  echo "<td style=\"background-color: #$color; border: 1px solid black;\">&nbsp;</td>\n";

		       $dt = IncDate($periode, $dt);
		    }
		 }

	         while (PerDateComp($dt, $dend) >= 0) {
		    if ($datum > 0 && $dt <= $datum) {
		       if (($wd_hours = GetWD_HOURS($db, $dt, $oressource, $ta_hash)) > 0) {
		          printf("<td class=\"selnum\">I:&nbsp;%s</td>\n", FormatNum($wd_hours, 3));
		          $sumip += $wd_hours;
		       } else
		          echo "<td class=\"sel\">&nbsp;</td>";
		    } else
		       echo "<td class=\"sel\">&nbsp;</td>";

	            $dt = IncDate($periode, $dt);
	         }

		 echo "<td class=\"selnum\"";

		 if ($datum > 0)
		    printf("<b>%s</b></td>", FormatNum($sumip, 3));
		 else
		    printf("><b>%s</b></td>", FormatNum($sum, 3));

		 $total += $sumip;
		 $sum = 0;
		 $sumi = 0;
		 $sumip = 0;
		 echo "</tr>\n";
	      }

	      # Starrer Zeilenbegin ...
	      echo "<tr>\n";
	      # Nummerierung
              echo "<td class=\"sel\">&nbsp;</td>\n";
	      # Ressource
	      echo "<td class=\"sel\" colspan=2 width=\"20%\"> </td>\n";
	      echo "<td class=\"sel\"><i>$am_nname $am_vname</i></td>\n";
	      # Tasklaenge mit Einheit
	      echo "<td class=\"sel\" colspan=3>&nbsp</td>\n";
	      # Mitarbeiterrolle
	      if ($role)
	         echo "<td class=\"sel\">$am_role</td>\n";

	      # Zellen bis zum Taskanfang schreiben
	      $query = "select ta_start, ta_duration, ta_hash from task where ta_num = $am_task";

	      if (!($res = QueryDB($db, $query))) {
	         Error("Keinen dazupassenden Task gefunden!");
	         return 0;
	      }

	      $data = fetchDB($res, 0);
	      $ta_start = $data[0];
	      $ta_duration = $data[1];
	      $ta_hash = $data[2];
	      $dt = $dstart;

	      while (PerDateComp($dt, $ta_start) > 0) {
	         if ($datum > 0 && $dt <= $datum) {
	            if (($wd_hours = GetWD_HOURS($db, $dt, $am_ressource, $ta_hash)) > 0) {
	               printf("<td class=\"selnum\">I:&nbsp;%s</td>\n", FormatNum($wd_hours, 3));
	               $sumip += $wd_hours;
		    } else
		       echo "<td class=\"sel\">&nbsp;</td>\n";
		 } else
	            echo "<td class=\"sel\">&nbsp;</td>";

	         $dt = IncDate($periode, $dt);
	      }

	      $oressource = $am_ressource;
	   }

	   # Rest der Zeile schreiben
	   $PerStart = GetPeriode($am_pstart);
	   $pyear = gmdate("Y", $am_pstart);
	   $PerAkt = GetPeriode($dt);
	   $tyear = gmdate("Y", $dt);

	   # Wurde ein gueltiges Datum angegeben, dann blenden wir
	   # die IST-Daten zum Task ein.
	   if ($datum > 0 && $dt <= $datum) {
	      $wd_hours = GetWD_HOURS($db, $dt, $am_ressource, $ta_hash);
	      $sumi += $wd_hours;
	   }

	   if ($PerStart == $PerAkt && $pyear == $tyear) {
              echo "<td style=\"background-color: #$color; text-align: right; border: 1px solid black;\">";

              if ($datum > 0 && $dt <= $datum)
		 printf("I:&nbsp;%s</td>\n", FormatNum($wd_hours, 3));
	      else if ($datum > 0)
		 printf("P:&nbsp;%s</td>\n", FormatNum($am_hours, 3));
	      else
		 printf("%s</td>\n", FormatNum($am_hours, 3));

	      $sum += $am_hours;

	      if ($datum > 0 && $dt > $datum)
	         $sumip += $am_hours;
	      else if ($datum > 0 && $dt <= $datum)
	         $sumip += $wd_hours;

	      $ergrow++;
	   } else if ($datum > 0 && $wd_hours > 0 && $dt <= $datum) {
	      printf("<td style=\"background-color: #$color; text-align: right; border: 1px solid black;\">I:&nbsp;%s</td>\n",
		FormatNum($wd_hours, 3));
	      $sumip += $wd_hours;
	   } else
	      echo "<td style=\"background-color: #$color; border: 1px solid black;\">&nbsp;</td>\n";

	   $dt = IncDate($periode, $dt);
	}

	if ($erganz > 0) {
	   $ed = GetEDate($einheit, $ta_start, $ta_duration);

	   if ($dt < $ed) {
	      while (PerDateComp($dt, $ed) >= 0) {
	         if ($datum > 0 && $dt <= $datum) {
	            $wd_hours = GetWD_HOURS($db, $dt, $am_ressource, $ta_hash);
		    $sumip += $wd_hours;

		    if ($wd_hours > 0)
		       printf ("<td style=\"background-color: #$color; border: 1px solid black;\">I:&nbsp;%s</td>\n",
			FormatNum($wd_hours, 3));
		    else
		       echo "<td style=\"background-color: #$color; border: 1px solid black;\">&nbsp;</td>\n";
		 } else
		    echo "<td style=\"background-color: #$color; border: 1px solid black;\">&nbsp;</td>\n";

	         $dt = IncDate($periode, $dt);
	      }
	   }

	   while (PerDateComp($dt, $dend) >= 0) {
	      if ($datum > 0 && $dt <= $datum) {
	         $wd_hours = GetWD_HOURS($db, $dt, $am_ressource, $ta_hash);
		 $sumip += $wd_hours;

		 if ($wd_hours > 0)
		    printf ("<td class=\"selnum\">I:&nbsp;%s</td>\n",
			FormatNum($wd_hours, 3));
		 else
		    echo "<td class=\"sel\">&nbsp;</td>\n";
	      } else
	         echo "<td class=\"sel\">&nbsp;</td>";

	      $dt = IncDate($periode, $dt);
	   }

	   echo "<td class=\"selnum\">";

	   if ($datum > 0)
	      printf("<b>%s</b></td>", FormatNum($sumip, 3));
	   else
	      printf("<b>%s</b></td>", FormatNum($sum, 3));

	   $total += $sumip;
	   echo "</tr>\n";
	}

	return $total;
}

# Folgende Funktion zeichnet einen Plan in einer Form, welche gedruckt
# werden kann.
# Es wird immer der Zeitraum vom Start des Projekts bis zu seinem Ende
# dargestellt. Bei sehr langen Projekten oder sehr kleinen Perioden, kann
# die Tabelle durchaus sehr breit werden.
#
function PrintPlan($pl_num) {
	global $rstufe;
	global $unum;
	global $periode;
	global $verteil;
	global $einheit;
	global $menu;
	global $dtformatshort;
	global $role;
	global $leneinheit;

	if ($pl_num <= 0) {
	   Error("Interner Fehler: Es wurde keine Plannummer an \"PrintPlan\" &uuml;bergeben!");
	   return;
	}

	if ($rstufe == 0 || $rstufe == 5 || $rstufe == 7) {
	   Error("Berechtigungsfehler!");
	   return;
	}

	$dt = $_REQUEST['datum'];

	if (isset($dt) && strlen($dt) > 0) {
	   $darr = explode(".", $dt);
	   $datum = gmmktime(0, 0, 0, $darr[0], daysinmonth($darr[0], $darr[1]), $darr[1]);
	} else
	   $datum = 0;

	$db = OpenDB();
	$query = "select pr_name, pl_prnum, mi_nname, mi_vname from ";
	$query .= "plan, project, mitarbeiter where pl_num = $pl_num and ";
	$query .= "pr_num = pl_prnum and mi_num = pr_pl";
	$result = QueryDB($db, $query);

	if (!$result) {
	   closeDB($db);
	   return;
	}

	$data = fetchDB($result, 0);
	$pr_name = $data[0];
	$pl_prnum = $data[1];
	$mi_nname = $data[2];
	$mi_vname = $data[3];
	echo "<table class=\"sel\"><tr><td class=\"selakt\"><table border=0>";
	echo "<tr><td>Projekt:</td><td>$pl_prnum $pr_name</td></tr>\n";
	echo "<tr><td>Projektleiter:</td><td>$mi_nname $mi_vname</td></tr>\n";
	# Anzeigen, bis zu welchem Datum IST-Buchungen beruecksichtigt
	# werden.
	if ($datum > 0) {
	   echo "<tr><td>IST&minus;Buchungen ber&uuml;cksichtigt bis:</td>";
	   echo "<td>" . GetShortDate($datum) . "</td></tr>\n";
	}
	
	echo "</table></tr></td></table>\n";
	# Zeichnen der fixen Kopfspalten.
	$nav = "pl_num=$pl_num&menu=$menu&headline=27";
?>
<table class="sel">
   <tr>
      <th class="sel" rowspan=2>Num.</th>
      <th class="sel" colspan=3 rowspan=2>
         <table border=0 width="100%"><tr><td align="center" colspan=2>Taskname</td></tr>
	    <tr><td>&nbsp;</td><td align="right">Ressource</td></tr>
	 </table>
      </th>
      <th class="sel" rowspan=2 colspan=3>
         <table border=0 width="100%"><tr><td align="center" colspan=3>Arbeit</td></tr>
	    <td align="center">Dauer</td>
	    <td align="center">Start</td>
	    <td align="center">Ende</td></tr>
	 </table>
      </th>
<?php
	if ($role)
	   echo "      <th class=\"sel\" rowspan=2>Rolle</th>\n";

	# Ermitteln des Projektstarts um den Tabellenkopf fertig zu schreiben.
	$query = "select ta_start, ta_duration, ta_meeting from task where ";
	$query .= "ta_plnum = $pl_num order by ta_start";
	$result = QueryDB($db, $query);

	if (!$result) {
	   closeDB($db);
	   return;
	}

	$numrows = numrowsDB($result);
	$row = 0;
	$fdate = 0;
	$ldate = 0;

	while ($row < $numrows) {
	   $data = fetchDB($result, $row);
	   $ta_start = $data[0];
	   $ta_duration = $data[1];
	   $ta_meeting = $data[2];

	   if ($row == 0)
	      $fdate = $ta_start;

	   if ($ta_meeting)
	      $ed = $ta_start;
           else
	      $ed = GetEDate($einheit, $ta_start, $ta_duration);

	   if ($ed > $ldate)
	      $ldate = $ed;

	   $row++;
	}

	# Erstellen des restlichen Tabellenkopfs ...
	$tm = gmdate("j.n.Y", $fdate);
	$dt = explode(".", $tm);
	$smon = $dt[1];
	$syear = $dt[2];
	$dt = $fdate;
	$cols = 0;
	$tbcols = 1;
	$oyear = $syear;

	while (PerDateComp($dt, $ldate) > 0) {
	   $syear = gmdate("Y", $dt);

	   if ($oyear != $syear) {
	      if ($cols <= 1)
	         echo "<th class=\"sel\">$oyear</th>\n";
	      else
	         echo "<th class=\"sel\" colspan=$cols>$oyear</th>\n";

	      $oyear = $syear;
	      $cols = 0;
	   }

	   $cols++;
	   $tbcols++;
	   $dt = IncDate($periode, $dt);
	}

	$cols++;

	if ($cols <= 1)
	   echo "<th class=\"sel\">$syear</th>\n";
	else
	   echo "<th class=\"sel\" colspan=$cols>$syear</th>\n";

	# Ueberschrift der letzten Spalte, welche die Summierungen der
	# geplanten Aufwaende enthaelt.
	echo "<th class=\"sel\" rowspan=2>Summe<br>Aufwand</th>\n";
	echo "</tr>\n";
	$dt = $fdate;

	while (PerDateComp($dt, $ldate) >= 0) {
	   printf ("<th class=\"sel\">%s</th>\n", GetStrMonth($smon));
	   $smon++;
	   $dt = IncDate($periode, $dt);

	   if ($smon > 12) {
	      $syear++;
	      $smon = 1;
	   }
	}

	echo "</tr>\n";

	# Nun muessen wir die Tabelle mit Inhalten fuellen
	$query = "select ta_num, ta_id, ta_name, ta_meeting, ta_start,";
	$query .= "ta_duration, ta_level, ta_phase, ta_color, ta_plnum ";
	$query .= "from task where ta_plnum = $pl_num order by ta_id";
	$result = QueryDB($db, $query);

	if (!$result) {
	   closeDB($db);
	   return;
	}

	$numrows = numrowsDB($result);
	$row = 0;
	$snum = "1";
	$ma = false;
        $done = false;
	$total = 0;

	while ($row < $numrows) {
	   if (!$ma) {
	      $data = fetchDB($result, $row);
	      $ta_num = $data[0];
	      $ta_id = $data[1];
	      $ta_name = $data[2];
	      $ta_meeting = $data[3];
	      $ta_start = $data[4];
	      $ta_duration = $data[5];
	      $ta_level = $data[6];
	      $ta_phase = $data[7];
	      $ta_color = $data[8];
	      $ta_plnum = $data[9];

	      # Der Task muss oben summiert sein, also in der Taskzeile
	      # selbst. Wurde jedoch ein Datum angegeben, dann muessen wir
	      # bis zum Datum die IST-Werte summieren und erst ab da die
	      # Planwerte.
	      if ($ta_level == 1) {
	         $query = "select ta_num, ta_level, ta_name from task where ";
	         $query .= "ta_plnum = $ta_plnum and ta_id > $ta_id and ";
	         $query .= "ta_meeting = 0 order by ta_id";

	         if (!($resta = QueryDB($db, $query))) {
	            closeDB($db);
	            return;
		 }

		 $men = numrowsDB($resta);
		 $i = 0;
		 $al_hours = 0;
		 $wd_hours = 0;

		 while ($i < $men) {
		    $data = fetchDB($resta, $i);
		    $xnum = $data[0];
		    $xlevel = $data[1];
		    $xname = $data[2];

		    if ($xlevel <= $ta_level)
		       break;

		    $query = "select sum(al_hours) from allocation where ";
		    $query .= "al_task = $xnum and al_pstart > $datum";

		    if (!($resal = QueryDB($db, $query))) {
		       closeDB($db);
		       return;
		    }

		    $data = fetchDB($resal, 0);
		    $al_hours += $data[0];
		    # Summieren der IST-Werte
		    $query = "select sum(wd_hours)/$leneinheit from wdone where ";
		    $query .= "wd_datum <= $datum and ";
		    $query .= "wd_task in (select ta_num from task, plan where ";
		    $query .= "pl_num = ta_plnum and ta_name = '$xname' and ";
		    $query .= "pl_prnum = $pl_prnum)";

		    if (!($reswd = QueryDB($db, $query)))
		       return 0;

		    $data = fetchDB($reswd, 0);
		    $wd_hours += $data[0];
		    $i++;
		 }
	      } else {
	         $query = "select sum(al_hours) from allocation where ";
		 $query .= "al_task = $ta_num and al_pstart > $datum";

		 if (!($resal = QueryDB($db, $query))) {
		    closeDB($db);
		    return;
		 }

		 $data = fetchDB($resal, 0);
		 $al_hours = $data[0];
		 # Summieren der IST-Werte
		 $query = "select sum(wd_hours)/$leneinheit from wdone where ";
		 $query .= "wd_datum <= $datum and ";
		 $query .= "wd_task in (select ta_num from task, plan where ";
		 $query .= "pl_num = ta_plnum and ta_name = '$ta_name' and ";
		 $query .= "pl_prnum = $pl_prnum)";

		 if (!($reswd = QueryDB($db, $query)))
		    return 0;

		 $data = fetchDB($reswd, 0);
		 $wd_hours = $data[0];
	      }
           }

	   # Starrer Zeilenbegin ...
	   echo "<tr>\n";
	   if (!$ma)
	      $snum = GetTaskNum($pl_num, $ta_id, $ta_level, $ta_phase, $ta_meeting, $snum, &$typ, $db);

	   # Nummerierung
	   if (!$ma) {
	      if ($ta_meeting)
	         echo "<td class=\"nwb\">$snum</td>\n";
	      else
	         echo "<td class=\"sel\">$snum</td>\n";
           } else {
              echo "<td class=\"sel\">&nbsp;</td>\n";
           }

	   # Tasknamendarstellung abhaenging vom Level
	   if (!$ma) {
	      if ($ta_level == 1 || !$typ) {
	         if ($ta_meeting)
	            echo "<td class=\"nwb\" colspan=3>$ta_name</td>\n";
	         else
	            echo "<td class=\"nw\" colspan=3>$ta_name</td>\n";
	      } else {
	         if ($ta_meeting)
	            echo "<td class=\"sel\" width=\"10%\"> </td><td class=\"nwb\" colspan=2>$ta_name</td>\n";
	         else
	            echo "<td class=\"sel\" width=\"10%\"> </td><td class=\"nw\" colspan=2>$ta_name</td>\n";
	      }
           }

	   # Anzeigen Spalte: Arbeit
	   if (!$ta_meeting) {
	      if ($typ) {
	         $sein = GetStrEinheit($einheit);
	         echo "<td class=\"nw\" align=\"right\">$ta_duration $sein</td>\n";
		 $dt = gmdate($dtformatshort, $ta_start);
		 $ed = GetEDate($einheit, $ta_start, $ta_duration);
		 $dte = gmdate($dtformatshort, $ed);
		 echo "<td class=\"selmid\">$dt</td>\n";
		 echo "<td class=\"selmid\">$dte</td>\n";
	      } else {
	         echo "<td class=\"sel\" colspan=3>&nbsp;</td>\n";
	      }
	   } else {
	      echo "<td class=\"nwb\" align=\"center\">M</td>\n";
	      $dt = gmdate($dtformatshort, $ta_start);
	      echo "<td class=\"selmid\" colspan=2>$dt</td>\n";
	   }

           # Spalte "Mitarbeierrolle" nur bei Mitarbeitern!
           if ($role)
              echo "<td class=\"sel\">&nbsp;</td>\n";

	   # Dynamischer Aufbau der restlichen Tabelle.
	   $color = dechex($ta_color);
	   $len = strlen($color);

	   while ($len < 6) {
	      $color = "0" . $color;
	      $len++;
	   }

	   $dt = $fdate;

	   # Die leeren Spalten vor Beginn eines Tasks zeichnen.
	   while (PerDateComp($dt, $ta_start) > 0) {
	      $dt = IncDate($periode, $dt);
	      echo "<td class=\"sel\">&nbsp;</td>";
	   }

	   if ($ta_meeting)
	      $edate = $ta_start;
	   else
	      $edate = GetEDate($einheit, $ta_start, $ta_duration);

	   # Den Zeitraum eines Tasks "grafisch" darstellen und den Aufwand
	   # pro Periode anzeigen.
	   do {
	      if ($typ)
	         echo "<td style=\"background-color: #$color; border: 1px solid black;\">&nbsp;</td>\n";
	      else if ($ta_meeting)
	         echo "<td style=\"color: #$color;background-color: #969696; border: 1px solid black;\"><center>###</center></td>\n";
	      else
	         echo "<td style=\"color: #$color;background-color: #dddddd; border: 1px solid black;\"><center>===</center></td>\n";

	      $dt = IncDate($periode, $dt);
           }
	   while (PerDateComp($dt, $edate) >= 0);

	   # Die leeren Spalten nach dem Ende eines Tasks zeichnen.
	   while (PerDateComp($dt, $ldate) >= 0) {
	      $dt = IncDate($periode, $dt);
	      echo "<td class=\"sel\">&nbsp;</td>";
	   }

	   # Ausfuellen der Summenspalte
	   if (($typ && !$ma) || $ta_level == 1) {
	      echo "<td class=\"two\" align=\"right\">";
	      printf ("<b><u>%s</u></b></td>\n", FormatNum($al_hours+$wd_hours, 3));
	   } else
	      echo "<td class=\"sel\">&nbsp;</td>\n";

	   if ($typ && !$ma) {
	      if ($ta_num == $tanum)
	         $sum = PrintMa($db, $fdate, $ldate, $ta_num, $color, $pl_num, $ressource);
	      else
	         $sum = PrintMa($db, $fdate, $ldate, $ta_num, $color, $pl_num, 0);

	      $total += $sum;
	   }

	   echo "</tr>\n";

	   if ($ma) {
              $ma = false;
              $done = true;
           }

	   if (!$done && $ta_num == $id)
	      $ma = true;

	   if (!$ma)
	      $row++;
	}

	# Anfuegen der Summierungen
	if ($datum == 0 && $total > 0) {
	   if ($role)
	      $span = 8 + $tbcols;
	   else
	      $span = 7 + $tbcols;

	   if (($typ && !$ma) || $ta_level == 1) {
	      echo "<tr><td class=\"sel\" colspan=$span>&nbsp;</td>";
	      echo "<td class=\"two\" align=\"right\">";
	      printf("<b><u>%s</u></b></td></tr>\n", FormatNum($al_hours+$wd_hours, 3));
           }

	   echo "<tr><td class=\"selnum\" colspan=$span>Gesamtsumme:</td>";
	   printf("<td class=\"selgray\" align=\"right\"><b><u>%s</u></b></td></tr>\n", FormatNum($total, 3));
	}

	echo "</table>\n";
	closeDB($db);
}

# Auswertung des Menues:
#
if ($knopf == 21)		// Statusbericht
   Preselect();

# Auswertung der Funktionen
if ($func == "printplan") {
   $pl_num = $_REQUEST['pl_num'];
   $datum = $_REQUEST['datum'];
   echo "<table class=\"indent\"><tr><td>\n";
   ButtonPrint("Drucken", "printplan.php", "pl_num=$pl_num&datum=$datum&header=2&func=PlanPrint");
   echo "</td></tr></table>\n";
   PrintPlan($pl_num);
}

if ($func == "PlanPrint") {
   $pl_num = $_REQUEST['pl_num'];

   PrintPlan($pl_num);
}

require('footer.inc');

?>
